package cn.jdx.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
[1].编写拦截器实现 HandlerInterceptor 接口 [look]->LoginInterceptor.java
[2].配置拦截器 [look]->springmvc-servlet.xml 需要指定自己编写的 拦截器类
[3].编写Controller (测试使用拦截器)
 */
public class InterceptorALL implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("[拦截器]正常运行...");
        //表示所有的域都可以接受
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许所有的方法
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT,OPTIONS");
        //请求得到结果的有效期
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Content-Type,access-control-allow-origin,version-info");
        //该值是一个boolean值,表示允许发送cookie，默认情况下cookie不包含在cores中，设置为true
        response.setHeader("Access-Control-Allow-Credentials", "true");
        //解决乱码
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        return true;//直接开启
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
}
